package lz.controls.managers
{
	import com.global.GlobalDef;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.utils.Dictionary;

	/**
	 * 弹出窗口管理
	 * @author lxh
	 * 
	 */	
	public class PopUpManager
	{
		/**
		 *窗口集合 
		 */		
		private static var windows 	:Dictionary = new Dictionary();
		private static var length 		:int = 0;
		/**
		 *背景遮罩组合 
		 */		
		public static var markBgMap 	:Dictionary = new Dictionary();
		private static var _window 	:DisplayObject;
		/**
		 *添加一个弹出框 
		 * @param window
		 * @param gStage
		 * @param gWidth
		 * @param gHeight
		 * @param modal 是否屏蔽背景，后方内容无法点击
		 * @return 
		 * 
		 */		
		public static function addPopUp(window :DisplayObject,
										parent :DisplayObjectContainer,
										gWidth:Number,
										gHeight:Number,
										modal:Boolean=true):DisplayObject
		{
			if(window==null ||parent==null )
			{
				return null;
			}
			var markBg :Sprite;
			if(parent && window)
			{
				if(modal)
				{
					markBg = new Sprite();
					markBg.graphics.clear();
					parent.addChild(markBg);
					markBg.graphics.beginFill(0x999999,0);
					markBg.graphics.drawRect(0,0,GlobalDef.GameStage.stageWidth,GlobalDef.GameStage.stageHeight);
					markBg.graphics.endFill();
					markBgMap[window] = markBg;
				}
				_window = window;
				window.x = (GlobalDef.GameStage.stageWidth - window.width)/2 + length * 5;
				window.y = (GlobalDef.GameStage.stageHeight - window.height)/2 + length * 5;
				if((window.x + window.width) >= gWidth)
				{
					window.x = (GlobalDef.GameStage.stageWidth - window.width)/2; 
					window.y = (GlobalDef.GameStage.stageHeight - window.height)/2;
				}
				parent.addChild(window);
				length ++ ;
				_window.addEventListener(Event.REMOVED_FROM_STAGE,onRemoveFromStage);
				///
				windows[_window] = _window;
			}
			return window;
		}
		
		/**
		 * 移除
		 * @param e
		 * 
		 */		
		private static function onRemoveFromStage(e :Event):void
		{
			_window = e.target as DisplayObject;
			if(_window)
			{
				if(markBgMap[_window] && markBgMap[_window].parent)
				{
					markBgMap[_window].parent.removeChild(markBgMap[_window]);
				}
				_window.removeEventListener(Event.REMOVED_FROM_STAGE,onRemoveFromStage);
				delete windows[_window];
				delete markBgMap[_window];
				_window = null;
				length --;
			}
		}
		/**
		 *添加一个窗口 
		 * @param window
		 * @param parent
		 * @param x
		 * @param y
		 * @param modal 是否屏蔽背景，后方内容无法点击
		 * @return 
		 */		
		public static function addWindow(window :DisplayObject,
										 parent :DisplayObjectContainer,
										 x:Number = 0,
										 y:Number = 0,
										 modal:Boolean = true):DisplayObject
		{
			var markBg :Sprite;
			if(parent && window)
			{
				if(modal)
				{
					markBg = new Sprite();
					markBg.graphics.clear();
					parent.addChild(markBg);
					markBg.graphics.beginFill(0x999999,0);
					markBg.graphics.drawRect(0,0,GlobalDef.GameStage.stageWidth,GlobalDef.StageHeight);
					markBg.graphics.endFill();
					markBgMap[window] = markBg;
				}
				_window = window;
				_window.x = x;
				_window.y = y;
				parent.addChild(_window);
				windows[_window] = _window;
				length ++ ;
				_window.addEventListener(Event.REMOVED_FROM_STAGE,onRemoveFromStage);
			}
			return _window;
		}
	}
}